//
//  LLAlertView.m
//  TaoTuo
//
//  Created by admin on 2019/10/28.
//  Copyright © 2019 admin. All rights reserved.
//

#import "LLAlertView.h"
#import <UIView+TYAlertView.h>

@interface LLAlertView ()

@property (nonatomic, strong) UIView *bgView;   //背景
@property (nonatomic, copy) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *topImageView;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *confirmBtn;


@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *topImageName;
@property (nonatomic, copy) NSString *message;
@property (nonatomic, copy) NSString *cancelTitle;
@property (nonatomic, copy) NSString *confirmTitle;
@property (nonatomic, copy) dispatch_block_t cancelBlock;
@property (nonatomic, copy) BOOL(^confirmBlock)(void);

@end

@implementation LLAlertView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
    }
    return self;
}

// 取消按钮点击
- (void)cancelBtnClick
{
    [self hideView];
    
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

// 确定按钮点击
- (void)confirmBtnClick
{
    if (self.confirmBlock) {
        BOOL dismiss = self.confirmBlock();
        if (dismiss) {
            [self hideView]; 
        }
    }
}

- (void)setupSubviews
{
    if (self.title.length > 0) {
        self.topImageView.hidden = YES;
        self.titleLabel.hidden = NO;
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.centerX.equalTo(self.bgView);
            make.height.mas_equalTo(40);
        }];
    }
    if (self.topImageName.length > 0) {
        self.topImageView.hidden = NO;
        self.titleLabel.hidden = YES;
        UIImage *image = self.topImageView.image;
        [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self.bgView);
            make.height.mas_equalTo(image.size.height * self.bgView.width / image.size.width);
        }];
    }
    MASViewAttribute *topAttr = self.title.length > 0 ? self.titleLabel.mas_bottom : self.topImageView.mas_bottom;
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topAttr).mas_offset(26.5);
        make.left.mas_equalTo(25);
        make.right.equalTo(self.bgView).mas_offset(-15);
    }];
    
    CGFloat leftSpace = 20 * kScale;
    if (self.cancelTitle.length > 0) {
        
        CGFloat btnW = (self.bgView.width - 4 * leftSpace) * 0.5;
        [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.messageLabel.mas_bottom).mas_offset(35);
            make.left.mas_equalTo(leftSpace);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(btnW);
        }];
        
        [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.bgView).mas_offset(-leftSpace);
            make.top.width.height.equalTo(self.cancelBtn);
            
        }];
    } else {
        [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.messageLabel.mas_bottom).mas_offset(35);
            make.left.mas_equalTo(leftSpace);
            make.right.equalTo(self.bgView).mas_offset(-leftSpace);
            make.height.mas_equalTo(30);
        }];
    }
    
    [self layoutIfNeeded];
    
    self.bgView.height = self.confirmBtn.bottom + 15;
    self.height = self.bgView.height + 20;
}

/// 标题是图片的 有两个按钮的alertView
+ (instancetype)alertViewWithTopImageName:(NSString *)imageName message:(NSString *)message cancelTitle:(NSString *)cancelTitle cancelBlock:(nullable dispatch_block_t)cancelBlock confirmTitle:(NSString *)confirmTitle confirmBlock:(nonnull BOOL (^)(void))confirmBlock
{
    LLAlertView *alertView = [[self alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    alertView.topImageName = imageName;
    alertView.message = message;
    alertView.cancelTitle = cancelTitle;
    alertView.confirmTitle = confirmTitle;
    alertView.cancelBlock = cancelBlock;
    alertView.confirmBlock = confirmBlock;
    
    [alertView setupSubviews];
    
    return alertView;
}



/// 标题是图片的 有一个按钮的alertView
+ (instancetype)alertViewWithTopImageName:(NSString *)imageName message:(NSString *)message confirmTitle:(NSString *)confirmTitle confirmBlock:(nonnull BOOL (^)(void))confirmBlock
{
    LLAlertView *alertView = [self alertViewWithTopImageName:imageName message:message cancelTitle:@"" cancelBlock:nil confirmTitle:confirmTitle confirmBlock:confirmBlock];
    return alertView;
    
}


/// 标题是文字的 有两个按钮的alertView
+ (instancetype)alertViewWithTitle:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle cancelBlock:(nullable dispatch_block_t)cancelBlock confirmTitle:(NSString *)confirmTitle confirmBlock:(nonnull BOOL (^)(void))confirmBlock
{
    LLAlertView *alertView = [[self alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    alertView.title = title;
    alertView.message = message;
    alertView.cancelTitle = cancelTitle;
    alertView.confirmTitle = confirmTitle;
    alertView.cancelBlock = cancelBlock;
    alertView.confirmBlock = confirmBlock;
    
    [alertView setupSubviews];
    
    return alertView;
}

/// 标题是文字的 有一个按钮的alertView
+ (instancetype)alertViewWithTitle:(NSString *)title message:(NSString *)message confirmTitle:(NSString *)confirmTitle confirmBlock:(nonnull BOOL (^)(void))confirmBlock
{
    LLAlertView *alertView = [self alertViewWithTitle:title message:message cancelTitle:@"" cancelBlock:nil confirmTitle:confirmTitle confirmBlock:confirmBlock];
    return alertView;
}


- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(30, 5, self.width - 60, self.height - 10)];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 10;
        [self addSubview:_bgView];
    }
    return _bgView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithTitle:self.title color:UIColorHex(333333) fontSize:18];
        [self.bgView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UIImageView *)topImageView
{
    if (!_topImageView) {
        _topImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.topImageName]];
        _topImageView.contentMode = UIViewContentModeScaleAspectFill;
        _topImageView.layer.masksToBounds = YES;
        [self.bgView addSubview:_topImageView];
    }
    return _topImageView;
}

- (UILabel *)messageLabel
{
    if (!_messageLabel) {
        _messageLabel = [UILabel labelWithTitle:@"" color:UIColorHex(565656) fontSize:12];
        _messageLabel.numberOfLines = 0;
        [self.bgView addSubview:_messageLabel];
        
        if (self.message.length > 0) {
            if ([self.message containsString:@"\\n"]) {
                self.message =[self.message stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
            }
            NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
            style.lineSpacing = 5;
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:self.message];
            [attr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, self.message.length)];
            _messageLabel.attributedText = attr;
        }
        
    }
    return _messageLabel;
}

- (UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton createButtonWithTitle:self.cancelTitle titleColor:UIColorHex(5A5A5A) titleFontSize:13 borderColor:UIColorHex(5A5A5A) cornerRadius:5 contentEdgeInsets:UIEdgeInsetsMake(9, 25, 9, 25) target:self action:@selector(cancelBtnClick)];
        [self.bgView addSubview:_cancelBtn];
    }
    return _cancelBtn;
}

- (UIButton *)confirmBtn
{
    if (!_confirmBtn) {
        _confirmBtn = [UIButton createButtonWithTitle:self.confirmTitle titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:13] backgroundColor:UIColorHex(5C21AE) cornerRadius:5 target:self action:@selector(confirmBtnClick)];
        [self.bgView addSubview:_confirmBtn];
    }
    return _confirmBtn;
}


@end
